function sendRequest(url, type, params, successFN, errorFN){
	$.ajax({
		url: url,
		type: type,
		data: params,
		success: function(res){
			if(successFN){
				successFN(res);
			}
		},
		error: function(err){
			if(errorFN){
				errorFN(err);
			}
		}
	});
}

function sendRequestWithFile(url, type, params, successFN, errorFN){
	$.ajax({
		url: url,
		data: params,
		processData: false,
		contentType: false,
		type: type,
		success: function(res){
			if(successFN){
				successFN(res);
			}
		},
		error: function(err){
			if(errorFN){
				errorFN(err);
			}
		}
	});
}

function getList(url, success, error){
	var params = {
		action: 'get'
	};
	sendRequest(url, 'GET', params, success, error);
}

/* ======================== DRAW REGION ========================*/
function drawFaculty(faculty){
	var href = FACULTY_DETAIL + '#' + faculty.Id;
	var logo = drawDiv('col-md-4 col-lg-4 col-sm-4 col-xs-6');
	logo.append(drawA(THUMBNAIL, href, '').append(drawImage(faculty.Image)));
	var info = drawDiv('col-md-8 col-lg-8 col-sm-8 col-xs-6');
	info.append(drawP(TEXT_MUTED, '').append(drawA('', href, faculty.Name)));
	info.append(drawP(TEXT_MUTED, faculty.Diploma));
	info.append(drawP(TEXT_MUTED, faculty.Department));
	
	var row = drawDiv('row');
	row.append(logo);
	row.append(info);
	var li = drawLi('form-group').append(row);
	return li;
}

function drawProject(project){
	var href = PROJECT_DETAIL + '#' + project.Id;
	var logo = drawDiv('col-md-4 col-lg-4 col-sm-4 col-xs-6');
	logo.append(drawA(THUMBNAIL, href, '').append(drawImage(project.Image)));
	var info = drawDiv('col-md-8 col-lg-8 col-sm-8 col-xs-6');
	info.append(drawP(TEXT_MUTED, '').append(drawA('', href, project.Title)));
	info.append(drawP(TEXT_MUTED, '').append(drawSpan(SPAN_CALENDAR, '')).append(drawSpan('', project.Date)));
	
	var row = drawDiv('row');
	row.append(logo);
	row.append(info);
	var li = drawLi('').append(row);
	return li;
}

function drawFirstNew(news){
	var href = NEWS_DETAIL + '#news#' + news.Id;
	var tempDiv = drawDiv('col-md-12');
	tempDiv.append(drawA(THUMBNAIL, href, '').append(drawImage(news.Image)));
	tempDiv.append($('<h3/>').append(drawA('', href, '').append(drawLabel(CONTROL_LABEL, news.Title))));
	tempDiv.append(drawP(TIME, '').append(drawSpan(SPAN_CALENDAR, '')).append(drawSpan('', news.Date)));
	tempDiv.append(drawP('form-control-static', news.Summary));
	return tempDiv;
}

function drawBigNew(news){
	var href = NEWS_DETAIL + '#news#' + news.Id;
	var tempDiv = drawDiv('col-md-6 col-lg-6 col-sm-6 col-xs-6');
	tempDiv.append(drawA(THUMBNAIL, href, '').append(drawImage(news.Image)));
	tempDiv.append(drawA('', href, '').append(drawLabel(CONTROL_LABEL, news.Title)));
	tempDiv.append(drawP(TIME, '').append(drawSpan(SPAN_CALENDAR, '')).append(drawSpan('', news.Date)));
	tempDiv.append(drawP('form-control-static', news.Summary));
	return tempDiv;
}

function drawSmallNew(news){
	var href = NEWS_DETAIL + '#news#' + news.Id;
	var li = drawLi('');
	li.append(drawA('', href, news.Title));
	li.append(drawP(TIME, '').append(drawSpan(SPAN_CALENDAR, '')).append(drawSpan('', news.Date)));
	return li;
}

function drawFirstEvents(events){
	var href = EVENTS_DETAIL + '#events#' + events.Id;
	var tempDiv = drawDiv('col-md-12');
	tempDiv.append(drawA(THUMBNAIL, href, '').append(drawImage(events.Image)));
	tempDiv.append($('<h3/>').append(drawA('', href, '').append(drawLabel(CONTROL_LABEL, events.Title))));
	tempDiv.append(drawP(TIME, '').append(drawSpan(SPAN_CALENDAR, '')).append(drawSpan('', events.Date)));
	tempDiv.append(drawP('form-control-static', events.Summary));
	return tempDiv;
}

function drawBigEvents(events){
	var href = EVENTS_DETAIL + '#events#' + events.Id;
	var tempDiv = drawDiv('col-md-6 col-lg-6 col-sm-6 col-xs-6');
	tempDiv.append(drawA(THUMBNAIL, href, '').append(drawImage(events.Image)));
	tempDiv.append(drawA('', href, '').append(drawLabel(CONTROL_LABEL, events.Title)));
	tempDiv.append(drawP(TIME, '').append(drawSpan(SPAN_CALENDAR, '')).append(drawSpan('', events.Date)));
	tempDiv.append(drawP('form-control-static', events.Summary));
	return tempDiv;
}

function drawSmallEvents(events){
	var href = EVENTS_DETAIL + '#events#' + events.Id;
	var li = drawLi('');
	li.append(drawA('', href, events.Title));
	li.append(drawP(TIME, '').append(drawSpan(SPAN_CALENDAR, '')).append(drawSpan('', events.Date)));
	return li;
}

function drawActivity(activity){
	var href = NEWS_DETAIL + '#activity#' + activity.Id;
	var tempDiv = drawDiv('col-md-4 col-sm-4 col-lg-4 col-xs-6');
	tempDiv.append(drawA(THUMBNAIL, href, '').append(drawImage(activity.Image)));
	tempDiv.append(drawA('', href, '').append(drawLabel(CONTROL_LABEL, activity.Title)));
	tempDiv.append(drawP(TIME, '').append(drawSpan(SPAN_CALENDAR, '')).append(drawSpan('', activity.Date)));
	return tempDiv;
}

function drawInformation(info){
	var href = NEWS_DETAIL + '#info#' + info.Id;
	var logo = drawDiv('col-md-4 col-lg-4 col-sm-4 col-xs-6');
	logo.append(drawA(THUMBNAIL, href, '').append(drawImage(info.Image)));
	var detail = drawDiv('col-md-8 col-lg-8 col-sm-8 col-xs-6');
	detail.append(drawP(TEXT_MUTED, '').append(drawA('', href, info.Title)));
	detail.append(drawP(TEXT_MUTED, '').append(drawSpan(SPAN_CALENDAR, '')).append(drawSpan('', info.Date)));
	
	var row = drawDiv('row');
	row.append(logo);
	row.append(detail);
	var li = drawLi('').append(row);
	return li;
}

function drawFirstInformation(info){
	var href = NEWS_DETAIL + '#info#' + info.Id;
	var tempReturn = drawA(THUMBNAIL, href, '');
	tempReturn.append(drawImage(info.Image));
	tempReturn.append(drawP('fix-bottom-title', info.Title));
	return tempReturn;
}

function drawBigInformation(info){
	var href = NEWS_DETAIL + '#info#' + info.Id;
	var tempDiv = drawDiv('col-md-4 col-sm-4 col-lg-4 col-xs-6');
	tempDiv.append(drawA(THUMBNAIL, href, '').append(drawImage(info.Image)));
	tempDiv.append(drawA('', href, '').append(drawLabel(CONTROL_LABEL, info.Title)));
	tempDiv.append(drawP(TIME, '').append(drawSpan(SPAN_CALENDAR, '')).append(drawSpan('', info.Date)));
	return tempDiv;
}
							
function drawP(pClass, html){
	return $('<p/>', {class: pClass, html: html});
}

function drawLi(liClass){
	return $('<li/>', {class: liClass});
}

function drawUl(ulClass){
	//<ul id='test' class="list-unstyled"></ul>
	return $('<ul/>', {class: ulClass});
}

function drawA(aClass, href, html){
	return $('<a/>', {class: aClass, href: href, html: html});
}

function drawDiv(dClass){
	return $('<div/>', {class: dClass});
}

function drawImage(src){
	return $('<img>', {src: src});
}

function drawSpan(spanClass, html){
	return $('<span/>', {class: spanClass, html: html});
}

function drawLabel(labelClass, html){
	return $('<label/>', {class: labelClass, html: html});
}

/* ======================== REQUEST REGION ========================*/

function displayFaculty(){
	var params = {
		action: 'getByLimit',
		fromFaculty: 0,
		toFaculty: 3
	};
	sendRequest(URL_FACULTY, 'GET', params, function(res){
		var data = JSON.parse(res);
		for(var index in data){
			$('#facultyList').append(drawFaculty(data[index]));
		}
	}, function(err){
	});
}

function displayProject(){
	var params = {
		action: 'getByLimit',
		fromProject: 0,
		toProject: 3
	};
	sendRequest(URL_PROJECT, 'GET', params, function(res){
		var data = JSON.parse(res);
		for(var index in data){
			$('#projectList').append(drawProject(data[index]));
		}
	}, function(err){
	});
}